package ywpt.service.service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import ywpt.core.dao.HibernateEntityDao;
import ywpt.model.CallRecord;
import ywpt.model.ClientInfo;
import ywpt.model.ClientKeshi;
import ywpt.model.Department;
import ywpt.model.PeopleInfo;
import ywpt.model.SysMsgInfo;
import ywpt.model.UsualQuestion;
import ywpt.service.work.SysMsgInfoService;
import ywpt.web.common.PublicFunction;

public class CallRecordServiceImpl extends HibernateEntityDao<CallRecord> implements CallRecordService{
	private SysMsgInfoService sysMsgInfoService;

	public SysMsgInfoService getSysMsgInfoService() {
		return sysMsgInfoService;
	}

	public void setSysMsgInfoService(SysMsgInfoService sysMsgInfoService) {
		this.sysMsgInfoService = sysMsgInfoService;
	}

	@Override
	public String createRecordCode() {
		SimpleDateFormat FORMAT = new SimpleDateFormat("yyyyMMdd");
		DetachedCriteria query = DetachedCriteria.forClass(CallRecord.class,"callRecord");
		query.add(Restrictions.like("recordCode",FORMAT.format(new Date())+"%"));
		String recordCode="";
		String code="";
		Integer maxId=new Integer(0);
		query.addOrder(Order.desc("id"));
		List<CallRecord> callRecords = this.getCallRecords(query);
		if ( callRecords.iterator().hasNext() )	
		{
			code = (callRecords.iterator().next().getRecordCode()).substring(8);
			maxId = new Integer(code);
		}
		recordCode = PublicFunction.getNextCode("", maxId, false,3); 
		query.setProjection(null);
		return FORMAT.format(new Date())+recordCode;
	}

	@Override
	public CallRecord getCallRecordById(Long id) {
		return this.get(id);
	}

	@Override
	public int getCallRecordCountByCriteria(DetachedCriteria query) {
		return this.getCountByCriteria(query);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<CallRecord> getCallRecords(DetachedCriteria query,
			int pageSize, int startIndex) {
		return this.findAllByCriteria(query, pageSize, startIndex);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<CallRecord> getCallRecords(DetachedCriteria query) {
		return this.findAllByCriteria(query);
	}

	@Override
	public void removeCallRecord(Long id) {
		super.removeById(id);
	}

	@Override
	public void saveCallRecord(CallRecord callRecord, Long peopleId, Long clientId, Long usualId,Long checkId,Long keshiId,Long departId) {
		if(peopleId!= null){
			callRecord.setPeople(this.get(PeopleInfo.class,peopleId));
		}
		if (clientId!=null) {
			callRecord.setClient(this.get(ClientInfo.class, clientId));
		}
		if(usualId != null){
			callRecord.setUsual(this.get(UsualQuestion.class, usualId));
		}
		if(checkId!= null){
			callRecord.setChecker(this.get(PeopleInfo.class,checkId));
		}
		if (keshiId!=null) {
			callRecord.setKeshi(this.get(ClientKeshi.class, keshiId));
		}
		if (departId!=null) {
			callRecord.setDepart(this.get(Department.class, departId));
		}
		super.save(callRecord);
		if (checkId!=null) {
			addSysMsg(callRecord);
		}
	}

	@Override
	public void updateCallRecord(CallRecord callRecord, Long peopleId, Long clientId, Long usualId,Long checkId,Long keshiId,Long departId) {
		if(peopleId!= null){
			callRecord.setPeople(this.get(PeopleInfo.class,peopleId));
		}
		if (clientId!=null) {
			callRecord.setClient(this.get(ClientInfo.class, clientId));
		}
		if(usualId != null){
			callRecord.setUsual(this.get(UsualQuestion.class, usualId));
		}
		if(checkId!= null){
			callRecord.setChecker(this.get(PeopleInfo.class,checkId));
		}
		if (keshiId!=null) {
			callRecord.setKeshi(this.get(ClientKeshi.class, keshiId));
		}
		if (departId!=null) {
			callRecord.setDepart(this.get(Department.class, departId));
		}
		super.update(callRecord);
		if (checkId!=null) {
			addSysMsg(callRecord);
		}
	}
	
	
	protected void addSysMsg(CallRecord callRecord) {
		sysMsgInfoService.invalidMsg(11, callRecord.getId());
		SysMsgInfo sysMsgInfo = new SysMsgInfo();
		sysMsgInfo.setMsgTitle("电话记录");
		sysMsgInfo.setMsgContent( callRecord.getPeople().getPeopleName()+"分派了一条电话记录！");
		sysMsgInfo.setReceiveUser(callRecord.getChecker().getId() +",");
		sysMsgInfo.setCreateDate(new Date());
		sysMsgInfo.setBusinessType(11);
		sysMsgInfo.setBusinessId(callRecord.getId());
		sysMsgInfo.setUrl("/call/record/edit-"+callRecord.getId()+".action");
		sysMsgInfo.setCreateDate(new Date());
		sysMsgInfo.setCreater(null);
		sysMsgInfoService.saveSysMsgInfo(sysMsgInfo);
	}

	@SuppressWarnings("unchecked")
	@Override
	public Boolean checkCode(String recordCode) {
		List<CallRecord> list = new ArrayList<CallRecord>();
		DetachedCriteria query = DetachedCriteria.forClass(CallRecord.class);
		query.add(Restrictions.like("recordCode", "%"+recordCode+"%"));
		list.addAll(this.findAllByCriteria(query));
		if (list.size()>0) {
			return true;
		} else {
			return false;
		}
		
	}
}
